# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 12:57:37 2018

@author: jiujiujiu
网址：链币Home

"""

'url = http://api.lianbihome.net/api/?s=home/Information/index'
'category_id: 1'
'post_id: 232'
from WriteData import writedata
from Tk import genearteMD5
import requests
import json
from cs import day
import time


def getHTMLText(url):
    try:
        header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
        r = requests.get(url, timeout=30, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常

        r.encoding = r.apparent_encoding

        html = r.text
        return html
    except Exception as e:
        return None


# print(get_data(url, data))

def get_data(url):
    dates = []
    html = getHTMLText(url)
    if not html:
        return dates
    d = json.loads(html)
    data = d['data']['list']
    for date in data:
        date = time.strftime("%Y-%m-%d %X", time.localtime(int(date['published_time'])))[:10]
        dates.append(date)
    return dates


def get_data1(url):
    html = getHTMLText(url)
    if not html:
        return None
    d = json.loads(html)
    data = d['data']
    return data


def count_process_one():  # 资讯
    d1 = 0
    d2 = 0
    d3 = 0
    url = 'http://api.lianbihome.net/api/?s=home/Information/category'
    datelist = get_data(url)
    if not datelist:
        return (0, 0, 0)
    for date in datelist:
        s = day(date)
        if s == 0:
            d1 += 1
        elif s == 1:
            d2 += 1
        elif s == 2:
            d3 += 1
    print('----链币Home资讯获取中----')
    return (d1, d2, d3)


def count_process_two():
    d1 = 0
    d2 = 0
    d3 = 0
    a = 1
    id = ''
    #    for dt in data:
    #        id = dt['id']
    #    print(len(data),id)

    while 1:
        s = d1 + d2 + d3
        url = 'http://api.lianbihome.net/api/?s=home/Information/newsflash_list&id={}'
        date1 = time.strftime("%Y-%m-%d %X", time.localtime())[:10]
        date2 = time.strftime("%Y-%m-%d %X", time.localtime())[:8] + str(int(date1[8:]) - 1)
        date3 = time.strftime("%Y-%m-%d %X", time.localtime())[:8] + str(int(date1[8:]) - 2)
        u = url.format(id)
        print('----链币Home快讯第{}页获取中----'.format(a))
        data = get_data1(u)
        if date1 in data.keys():
            for dt in data[date1]:
                id = dt['id']
            d1 += len(data[date1])
        elif date2 in data.keys():
            for dt in data[date2]:
                id = dt['id']
            d2 += len(data[date2])
        elif date3 in data.keys():
            for dt in data[date3]:
                id = dt['id']
            d3 += len(data[date3])
        if s == (d1 + d2 + d3):
            break
        a += 1
    return (d1, d2, d3)


def main():
    u = '链币Home'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
